package com.ys.web.pay.model.po;

import com.ys.base.EnumType;
import com.ys.utils.base.http.McodeHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import lombok.Data;

/**
 * 发红包
 */
@Data
public class RedPacketCreate extends WalletBase{
    //红包类型
    public int packetType;
    public String singleAmount;
    public int packetCount;

    public void check(){
//        if(StringHandler.isEmpty(this.getPayPassword())){
//            SystemError.wrapBs("400","请输入密码");
//        }
        if(this.packetCount<=0){
            SystemError.wrapBs("400","红包金额不能为0");
        }
        long one = StringHandler.getLong(Config.pp.get("wallet.red.money.one"));
        long allMax = StringHandler.getLong(Config.pp.get("wallet.red.money.all.max"));
        if(StringHandler.isEmpty(singleAmount)){
            this.singleAmount = "0";
        }if(StringHandler.isEmpty(this.getAmount())){
            this.setAmount("0");
        }
        //私聊红包
        if(this.packetType==1 && (StringHandler.parseMoney(this.singleAmount)==0 || StringHandler.parseMoney(this.singleAmount)>one || this.packetCount!=1)){
            SystemError.wrapBs("400","金额错误.");
        }
        //拼手气红包
        if(this.packetType==2 &&
                (StringHandler.parseMoney(this.getAmount())==0 || this.packetCount>100 || StringHandler.parseMoney(this.getAmount())>allMax) || (StringHandler.parseMoney(this.getAmount())/this.packetCount)>one){
            SystemError.wrapBs("400","金额错误..");
        }
        //普通群红包
        if(this.packetType==3 &&
                (StringHandler.parseMoney(this.singleAmount)==0 || this.packetCount>100 || StringHandler.parseMoney(this.singleAmount)>one || (StringHandler.parseMoney(this.singleAmount)*this.packetCount)>allMax)){
            SystemError.wrapBs("400","金额错误...");
        }
        //专属红包
        if(this.packetType==4 && (StringHandler.parseMoney(this.singleAmount)==0 || StringHandler.parseMoney(this.singleAmount)>one || this.packetCount!=1)){
            SystemError.wrapBs("400","金额错误....");
        }
        this.singleAmount = String.valueOf(StringHandler.yuanToFen(this.singleAmount));
        if(this.getGp().equals("1")){
            this.setReceivedId(StringHandler.getString(McodeHandler.from32Radix(this.getReceivedId())));
            this.setAmount(this.singleAmount);
        }else {
            if(this.getAmount().equals("0")){
                this.setAmount(StringHandler.getString(StringHandler.getInt(this.singleAmount) * this.packetCount));
            }else {
                this.setAmount(String.valueOf(StringHandler.yuanToFen(this.getAmount())));
            }
        }
        if(this.getGp().equals("2") && StringHandler.isNotEmpty(this.getReceivedIds())){
            this.packetType = 4;
        }
        this.setType(EnumType.MessageCode.MESSAGE_RED_PACK.getCode());
        if(this.packetType==4){
            this.setReceivedIds(StringHandler.getString(McodeHandler.from32Radix(this.getReceivedIds())));
        }
        if(this.payChannel<1){
            SystemError.wrapBs("400","请选择钱包类型....");
        }
    }

    public int getPacketType() {
        return packetType;
    }

    public void setPacketType(int packetType) {
        this.packetType = packetType;
    }

    public String getSingleAmount() {
        return singleAmount;
    }

    public void setSingleAmount(String singleAmount) {
        this.singleAmount = singleAmount;
    }

    public int getPacketCount() {
        return packetCount;
    }

    public void setPacketCount(int packetCount) {
        this.packetCount = packetCount;
    }
}
